package com.sun.temp.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.temp.bean.FilePath;
import com.sun.temp.service.FilePathService;
import com.sun.temp.service.MaterialService;
import com.sun.temp.utils.PLCUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.HashMap;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import static java.lang.Thread.sleep;

//@Configuration
//@EnableScheduling
//@Slf4j
//public class ReadTask {
//
//    @Autowired
//    private MaterialService materialService;
//
//    @Autowired
//    private FilePathService filePathService;
//
//    private HashMap<String, String> cacheMap = new HashMap<>();
//
//    // @Scheduled(fixedRate=1000*60*10)//10分钟
//    @Scheduled(fixedRate=1000*10)//10秒钟
//    private void readTask()throws IOException{
//        cacheMap.clear();
//        init();
//    }
//    @PostConstruct
//    public void init() throws IOException {
//        String folderPath = filePathService.getOne(new QueryWrapper<FilePath>().eq("type", "1")).getFilepath();
//        File file = new File(folderPath);
//        File[] files = file.listFiles();
//        String filename = "";
//        if (files != null && files.length > 0) {
//            for (File f :files) {
//                filename = f.getName();
////                log.info("处理文件变化事件=======================================================" + filename);
//                // 判断文件后缀.AutoBackup
//                if (filename.toLowerCase().contains("backup")) {
//                    continue;
//                }
//
//                // 处理文件变化事件
//                Path changedPath = Paths.get(folderPath + "\\" + filename);
////                log.info("数据处理保存=======================================================");
//                if( materialService.saveAutoMaterial(changedPath.toFile())) {
//                    // 与机器人交互，启动下料任务
//                    // 为6-加热完毕加热炉下降
//                    PLCUtil.writeInt16("VW1002", 6);
//                    log.info("为6-加热完毕加热炉下降");
//                }
//            }
//        }
//    }
//
//
////    @PostConstruct
////    public void init() throws IOException {
////        String folderPath = filePathService.getOne(new QueryWrapper<FilePath>().eq("type", "1")).getFilepath();
////        Path path = Paths.get(folderPath);
////        WatchService watchService = FileSystems.getDefault().newWatchService();
////        path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
////
////        new Thread(() -> {
////            while (true) {
////                WatchKey key;
////                try {
////                    key = watchService.take();
////                } catch (InterruptedException e) {
////                    return;
////                }
////
////                for (WatchEvent<?> event : key.pollEvents()) {
////                    WatchEvent.Kind<?> kind = event.kind();
////                    System.out.println("File kind: " + kind);
////                    if (kind == StandardWatchEventKinds.OVERFLOW) {
////                        // 如果不是新增，则跳出
////                        continue;
////                    }
////
////                    String fileNmae = event.context().toString();
////                    // 判断文件后缀.AutoBackup
////                    if (fileNmae.toLowerCase().contains("backup")) {
////                        continue;
////                    }
////
////                    if (cacheMap.get(fileNmae) != null && cacheMap.get(fileNmae).equals(fileNmae)) {
////                        return;
////                    } else {
////                        cacheMap.put(fileNmae, fileNmae);
////                    }
////
////                    // 处理文件变化事件
////                    log.info("处理文件变化事件=======================================================");
////                    Path changedPath = Paths.get(folderPath + "\\" + event.context().toString());
////                    log.info("数据处理保存=======================================================");
////                    if( materialService.saveAutoMaterial(changedPath.toFile())) {
////                        // 与机器人交互，启动下料任务
////                        // 为6-加热完毕加热炉下降
////                        PLCUtil.writeInt16("VW1002", 6);
////                        log.info("为6-加热完毕加热炉下降");
////                    }
////                }
////
////                boolean valid = key.reset();
////                if (!valid) {
////                    break;
////                }
////            }
////        }).start();
////    }
//}